package jr.dao.impl;

import jr.dao.IBookInfoDao;
import jr.pojo.BookInfo;
import jr.util.DBHelper;
import jr.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;

public class BookInfoDaoImpl implements IBookInfoDao {

    DBHelper db=new DBHelper();

    @Override
    public int selectPageDateCount() {
        StringBuilder sql=new StringBuilder("select count(bookid) from book_info");

        Connection con=null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        try{
            con=db.getCon();
            ps=con.prepareStatement(sql.toString());
            resultSet=ps.executeQuery();
            if (resultSet.next()){
                return resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            db.closeAll(con,ps,resultSet);
        }

        return 0;
    }

    @Override
    /*bookcode,booktype,bookname,bookauthor,publishpress,isborrow*/
    public ArrayList<BookInfo> selectIndexDate(PageHelper ph) {
        String sql="select * from book_info order by creationtime desc limit ?,?";
        int index=(ph.getPageIndex()-1)*ph.getShowNumber();
        return db.dtQuery(sql,BookInfo.class,index,ph.getShowNumber());
    }

    @Override
    public ArrayList selectPageDateCountLike(BookInfo bi) {
        LinkedList ll=new LinkedList();
        StringBuilder sql=new StringBuilder("select * from book_info where 1=1");/*+" order by creationtime desc limit ?,?"*/
        if (bi.getBookname()!=""){
            sql.append(" and bookname like ? ");
            ll.add("%"+bi.getBookname()+"%");
        }
        if(bi.getBooktype()!=0){
            sql.append(" and booktype=? ");
            ll.add(bi.getBooktype());
        }
        if(bi.getIsborrow()!=-1){
            sql.append(" and isborrow=? ");
            ll.add(bi.getIsborrow());
        }
        sql.append(" order by creationtime desc");
        return db.dtQuery(sql.toString(),BookInfo.class,ll.toArray());
    }

    @Override
    public ArrayList<BookInfo> selectIndexDateLike(BookInfo bi, PageHelper ph) {
        LinkedList ll=new LinkedList();
        StringBuilder sql=new StringBuilder("select * from book_info where 1=1");/*+" order by creationtime desc limit ?,?"*/
        int index=(ph.getPageIndex()-1)*ph.getShowNumber();
        if (bi.getBookname()!=""){
          sql.append(" and bookname like ? ");
            ll.add("%"+bi.getBookname()+"%");
        }
        if(bi.getBooktype()!=0){
            sql.append(" and booktype=? ");
            ll.add(bi.getBooktype());
        }
        if(bi.getIsborrow()!=-1){
            sql.append(" and isborrow=? ");
            ll.add(bi.getIsborrow());
        }
        sql.append(" order by creationtime desc limit ?,?");
        ll.add((ph.getPageIndex()-1)*ph.getShowNumber());
        ll.add(ph.getShowNumber());
        return db.dtQuery(sql.toString(),bi.getClass(),ll.toArray());
        //return db.dtQuery(sql,BookInfo.class,index,ph.getShowNumber());


        /*LinkedList ll=new LinkedList();
        StringBuilder sql=new StringBuilder("select * from standard where 1=1 ");
        if(st.getStd_num()!=null){
            sql.append("and std_num like ? ");
            ll.add("%"+st.getStd_num()+"%");
        }
        if(st.getZhname()!=null){
            sql.append("and zhname like ? ");
            ll.add("%"+st.getZhname()+"%");
        }
        sql.append("limit ?,?");
        ll.add((ph.getPageIndex()-1)*ph.getShowNumber());
        ll.add(ph.getShowNumber());
        return db.dtQuery(sql.toString(),st.getClass(),ll.toArray());*/
    }

}
